// index.js
Page({
  data: {
    searchText: '',
    wordList: [
      { id: 1, english: 'abandon', chinese: '放弃，抛弃' },
      { id: 2, english: 'ability', chinese: '能力，才能' },
      { id: 3, english: 'abroad', chinese: '在国外' },
      { id: 4, english: 'absolute', chinese: '绝对的，完全的' },
      { id: 5, english: 'absorb', chinese: '吸收，吸引' },
      { id: 6, english: 'abstract', chinese: '抽象的，深奥的' },
      { id: 7, english: 'academic', chinese: '学术的，教学的' },
      { id: 8, english: 'accept', chinese: '接受，承认' },
      { id: 9, english: 'access', chinese: '通道，接近' },
      { id: 10, english: 'accident', chinese: '事故，意外' }
    ],
    filteredWords: [] // 初始化为空，在onLoad中设置初始值
  },

  onLoad: function() {
    // 初始化filteredWords为全部单词
    this.setData({
      filteredWords: this.data.wordList
    });
  },

  // 搜索输入事件处理
  onSearchInput: function(e) {
    const searchText = e.detail.value;
    this.setData({
      searchText: searchText
    });
    this.filterWords();
  },

  // 过滤单词
  filterWords: function() {
    const keyword = this.data.searchText.toLowerCase();
    if (!keyword) {
      this.setData({
        filteredWords: this.data.wordList
      });
      return;
    }
    
    const filtered = this.data.wordList.filter(word => 
      word.english.toLowerCase().includes(keyword) || 
      word.chinese.includes(keyword)
    );
    
    this.setData({
      filteredWords: filtered
    });
  },

  // 跳转到单词详情页
  goToDetail: function(e) {
    const wordId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/word-detail/index?id=${wordId}`
    });
  }
});